package com.scx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.scx.constant.StatusConstant;
import com.scx.dto.SetmealDTO;
import com.scx.dto.SetmealPageQueryDTO;
import com.scx.entity.Setmeal;
import com.scx.entity.SetmealBook;
import com.scx.exception.DeletionNotAllowedException;
import com.scx.mapper.SetmealBookMapper;
import com.scx.mapper.SetmealMapper;
import com.scx.result.PageResult;
import com.scx.service.SetmealService;
import com.scx.vo.BookItemVO;
import com.scx.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.scx.constant.MessageConstant.SETMEAL_ON_SALE;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealBookMapper setmealBookMapper;


    @Override
    @Transactional
    public void saveWithBook(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //保存套餐和图书的关联关系
        List<SetmealBook> setmealBookes = setmealDTO.getSetmealBookes();
        setmealBookes.forEach(setmealBook -> {
            setmealBook.setSetmealId(setmeal.getId());
            setmealBookMapper.insert(setmealBook);
        });
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //启售的套餐不能删除
        List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);
        setmeals.forEach(setmeal -> {
            if(Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(SETMEAL_ON_SALE);
            }
        });

        //批量删除套餐
        setmealMapper.deleteBatchIds(ids);
        //删除套餐图书关系
        LambdaQueryWrapper<SetmealBook> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealBook::getSetmealId, ids);
        setmealBookMapper.delete(lqw);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateById(setmeal);
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO  = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        LambdaQueryWrapper<SetmealBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealBook::getSetmealId, id);
        List<SetmealBook> setmealBookes = setmealBookMapper.selectList(lqw);
        setmealVO.setSetmealBookes(setmealBookes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateById(setmeal);

        //删除套餐和图书的关联关系,重新插入
        LambdaQueryWrapper<SetmealBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealBook::getSetmealId, setmealDTO.getId());
        setmealBookMapper.delete(lqw);
        setmealDTO.getSetmealBookes().forEach(setmealBook -> {
            setmealBook.setSetmealId(setmealDTO.getId());
            setmealBookMapper.insert(setmealBook);
        });
    }

    @Override
    public List<Setmeal> list(Long categoryId) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, categoryId);
        lqw.eq(Setmeal::getStatus,StatusConstant.ENABLE);
        return setmealMapper.selectList(lqw);
    }

    @Override
    public List<BookItemVO> getBookItemBySetmealId(Long setmealId) {
        //多表查询自己写sql
        return setmealBookMapper.getBookItemBySetmealId(setmealId);
    }
}
